package com.monkey.demolist.tree;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.monkey.demolist.R;
import com.monkey.demolist.tree.adapter.SimpleTreeViewAdapter;
import com.monkey.demolist.tree.adapter.TreeListViewAdapter;
import com.monkey.demolist.tree.bean.FileBean;
import com.monkey.demolist.tree.bean.Node;

import java.util.ArrayList;
import java.util.List;

/**
* Description: 任意层级树形图
* Author: Arthor
* Date: 2017/10/20
* Time: 16:53
* function:
*      1、树形结构的数据组织
*      2、反射+注解的使用(效率低，可以通过好的命名规范避免)
*      3、Adapter的封装
*/

public class TreeViewActivity extends AppCompatActivity {

    private ListView listView;

    private SimpleTreeViewAdapter adapter;

    private List<FileBean> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_view);

        listView = (ListView) findViewById(R.id.listview);

        initdata();


        try {
            adapter = new SimpleTreeViewAdapter(listView, this, mDatas, 1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        listView.setAdapter(adapter);

        initEvent();
    }

    private void initEvent() {
        adapter.setOnTreeNodeClickListenr(new TreeListViewAdapter.OnTreeNodeClickListner() {
            @Override
            public void onClick(Node node, int position) {
                if (node.isLeaf()){
                    Toast.makeText(TreeViewActivity.this,node.getName(),Toast.LENGTH_SHORT).show();
                }
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                final EditText editText = new EditText(TreeViewActivity.this);

                new AlertDialog.Builder(TreeViewActivity.this)
                        .setTitle("add node")
                        .setView(editText)
                        .setPositiveButton("sure", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (TextUtils.isEmpty(editText.getText())) return;
                                adapter.addExtraNode(position,editText.getText().toString());

                            }
                        })
                        .setNegativeButton("canle", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();

                return true;
            }
        });
    }

    private void initdata() {
        mDatas = new ArrayList<>();
        FileBean bean = new FileBean(1,0,"根目录1");
        mDatas.add(bean);
        bean = new FileBean(2,0,"根目录2");
        mDatas.add(bean);
        bean = new FileBean(3,0,"根目录3");
        mDatas.add(bean);

        bean = new FileBean(4,1,"根目录1-1");
        mDatas.add(bean);
        bean = new FileBean(5,1,"根目录1-2");
        mDatas.add(bean);
        bean = new FileBean(6,5,"根目录1-2-1");
        mDatas.add(bean);

        bean = new FileBean(7,3,"根目录3-1");
        mDatas.add(bean);

        bean = new FileBean(8,3,"根目录3-2");
        mDatas.add(bean);

        bean = new FileBean(9,7,"根目录3-1-1");
        mDatas.add(bean);
    }
}
